<?php


namespace App\controller;

use App\BaseController;
use think\facade\Db;
use Extend\facade\Config;
use Extend\facade\httpClient;

class Test extends BaseController{
    public function Index() {
        return '测试控制器，记录一些测试案例'
    }

    // 请求异常不报错中止
    public function guzzleExample() {
        // 不捕获异常，请求出错即退出程序
        $url = "https://www.baidu.com";
        $response = httpClient::get($url);
        $code = $response->getStatusCode(); // 200
        $reason = $response->getReasonPhrase(); // OK
        print_r(array(
            'code'      => $code,
            'reason'    => $reason
        ));
        if ($code != 200 && $reason != 'OK') {
            return false;
        }
        $bodyContents = $response->getBody()->getContents();
        print_r($bodyContents);

        // 捕获异常，请求出错自行处理不中止程序
        $url = "https://www.baidu.com/_err_.html";
        try {
            $response = $this->httpClient->get($url);
        }
        catch(\GuzzleHttp\Exception\RequestException $e) {
            $code = $e->getCode();
            return 404;
        }
        $bodyContents = $response->getBody()->getContents();
        print_r($bodyContents);
    }

    // 获取console参数
    public function console() {
        return Config::get('console');
    }

    // 调用逻辑层函数
    public function logic() {
        $logicTest = new \App\logic\Test();
        return $logicTest->test();
    }

    // 私有
    protected function protectedFunction() {
        return 'I am a protected method';
    }

    // 测试PredisFacade
    public function predisFacade() {
        $key = 'slongTest';
        $r = \App\facades\PredisFacade::get($key);
        var_dump($r);
        \App\facades\PredisFacade::setex('slongTest', 100, 'this is test');
    }
}